//******************************************************************************
// RO_PINOSC_TA0_WDTp_Wheel_Buttons example
// Touch the five wheel buttons to turn on/off the center button LED
// RO method capactiance measurement using PinOsc IO, TimerA0, and WDT+
//
//          Schematic Description: 
// 
//                         MSP430G2452
//                      +---------------+
//                      |
//             C--------|P2.4
//           C----------|P2.1
//               C------|P2.3
//             C--------|P2.2
//                      |
//           C----------|P2.5
//                      |
//           C----------|P2.0
// 
//        The WDT+ interval represents the measurement window.  The number of 
//        counts within the TA0R that have accumulated during the measurement
//        window represents the capacitance of the element. This is lowest 
//        power option with either LPM3 (ACLK WDTp source) or LPM0 (SMCLK WDTp 
//        source).
//
//******************************************************************************

#include "CTS_Layer.h"
#include  "msp430g2452.h"
int past_value,i=0,j,k;
int dimmer=0;
int start=0,stop=0;
int khoidong = 1;
int dem=0,dem1=0,flag=0,m=0;
void main(void)
{


 	WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
	  BCSCTL1 = CALBC1_1MHZ;                // Set DCO to 1, 8, 12 or 16MHz
	  DCOCTL = CALDCO_1MHZ;
	  BCSCTL1 |= DIVA_0;                    // ACLK/1 [ACLK/(0:1,1:2,2:4,3:8)]
	  BCSCTL2 |= DIVS_0;                    // SMCLK/8 [SMCLK/(0:1,1:2,2:4,3:8)]
	  BCSCTL3 |= LFXT1S_2;                  // LFXT1 = VLO Clock Source

  P1DIR |= BIT2;                        // Unused pins as outputs
  P1OUT = 0x00;
  P1SEL &= ~(BIT6 + BIT7);
   P2SEL &= ~(BIT6 + BIT7);
  P2DIR = 0xFF;
  P2OUT = 0x00;

  P1REN |= BIT0;                            // P1.4 pullup
  P1IE |= BIT0;                            // P1.4 interrupt enabled
  P1IES &= ~BIT0;                           // P1.4 Hi/lo edge
	P1IFG &= ~BIT0;                          // P1.4 IFG cleared
	//thiet lap ngat cong 1.4
	 P1REN |= BIT4;                            // P1.4 pullup
	  P1IE |= BIT4;                            // P1.4 interrupt enabled
	  P1IES &= ~BIT4;                          // P1.4 lo/HI edge
		P1IFG &= ~BIT4;                          // P1.4 IFG cleared
	//khai bao ngat cong 2.0
	  P1REN |= BIT6;                            // P1.4 pullup
  P1IE |= BIT6;                            // P1.4 interrupt enabled
  P1IES |= BIT6;                           // P1.4 Hi/lo edge
	P1IFG &= ~BIT6;                          // P1.4 IFG cleared
	//khai bao ngat cong 2.1
	  P1REN |= BIT7;                            // P1.4 pullup
  P1IE |= BIT7;                            // P1.4 interrupt enabled
  P1IES |= BIT7;                           // P1.4 Hi/lo edge
	P1IFG &= ~BIT7;                          // P1.4 IFG cleared
	for(i=0;i<1000;i++);
	_BIS_SR(GIE);
	while(1)
	{
		if(flag==1)
		{
			flag=0;
			for(i=0;i<135;i++);
			if((start==1)&&(stop==0))
					{
					if(khoidong==1)
					{
							start = 0;
							khoidong=0;
							dimmer = 70;
					}
					else
					{
							start=0;
							dimmer = past_value;
					}
					}
				if(((start==0)&&(stop==1))|((start==0)&&(stop==0)&&(khoidong==1)))
				{
					 P1OUT &= ~BIT2;
				}
				else
				{
					if(dimmer==120)m=4;
					else m=1;
					P1OUT |= BIT2;                            // P1.0 = toggle
				   TA0CCR0 = dimmer;
				   TA0CTL = TASSEL_1+MC_1+TACLR;
				   TA0CCTL0 &= ~CCIFG;
				   TA0CCTL0 |= CCIE;
				}
				  //P1IFG &= ~BIT0;
		}
	}
}

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
	if(P1IFG&BIT4)
					{
						if(P1IES&BIT4)
							{
							stop = 1;
							start = 0;
							past_value = dimmer;
							}
						else
							{
							start = 1;
							stop = 0;
							dimmer = 0;
							}
						P1IES ^= BIT4;                            // P1.4 Hi/lo edge
						P1IFG &= ~BIT4;
					}
		if(P1IFG&BIT7)//GIAM
		{
			//if(dimmer==30)dimmer = 20;
			//if(dimmer==40)dimmer = 30;
			if(dimmer==50)dimmer = 40;
			if(dimmer==60)dimmer = 50;
			if(dimmer==70)dimmer = 60;
			if(dimmer==80)dimmer = 70;
			if(dimmer==90)dimmer = 80;
			if(dimmer==100)dimmer = 90;
			if(dimmer==110)dimmer = 100;
			if(dimmer==120)dimmer = 110;

			P1IES^=BIT7;
			P1IFG &= ~BIT7;
		}
			if(P1IFG&BIT6)//TANG
		{
			if(dimmer==110)dimmer = 120;
			if(dimmer==100)dimmer = 110;
			if(dimmer==90)dimmer = 100;
			if(dimmer==80)dimmer = 90;
			if(dimmer==70)dimmer = 80;
			if(dimmer==60)dimmer = 70;
			if(dimmer==50)dimmer = 60;
			if(dimmer==40)dimmer = 50;
		//	if(dimmer==30)dimmer = 40;
		//	if(dimmer==20)dimmer = 30;

			P1IES^=BIT6;
			P1IFG &= ~BIT6;
		}

				if(P1IFG&BIT0)
							{
					flag = 1;
					P1IFG &= ~BIT0;
							/*if((start==1)&&(stop==0))
									{
									if(khoidong==1)
									{
											start = 0;
											khoidong=0;
											dimmer = 70;
									}
									else
									{
											start=0;
											dimmer = past_value;
									}
									}
								if(((start==0)&&(stop==1))|((start==0)&&(stop==0)&&(khoidong==1)))
								{
									 P1OUT &= ~BIT2;
								}
								else
								{
									if(dimmer==120)m=4;
									else m=1;
									P1OUT |= BIT2;                            // P1.0 = toggle
								   TA0CCR0 = dimmer;
								   TA0CTL = TASSEL_1+MC_1+TACLR;
								   TA0CCTL0 &= ~CCIFG;
								   TA0CCTL0 |= CCIE;
								 // P1IFG &= ~BIT0;                           // P1.4 IFG cleared
								}
								  P1IFG &= ~BIT0;*/
							}
				  _BIS_SR(GIE);
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void ISR_Timer0_A0(void)
{

 switch(m)
 {
 case 1:
	   P1OUT &= ~BIT2;
	   m = 2;	                          // P1.0 = toggle
	   TA0CCR0 = 130-dimmer;
	   TA0CTL = TASSEL_1+MC_1+TACLR;
	   TA0CCTL0 &= ~CCIFG;
	   TA0CCTL0 |= CCIE;

 break;
 case 2:
	P1OUT |= BIT2;
 	 m = 3;
   TA0CCR0 = dimmer;
   TA0CTL = TASSEL_1+MC_1+TACLR;
   TA0CCTL0 &= ~CCIFG;
   TA0CCTL0 |= CCIE;
 break;
 case 3:
	    P1OUT &= ~BIT2;
    m = 0;                            // P1.0 = toggle
	TA0CCR0 = 0;
	TA0CTL = 0;
	TA0CCTL0 = 0;
	 P1IFG &= ~BIT0;
 break;
 case 4:
	 P1OUT |= BIT2;
		TA0CCR0 = 0;
		TA0CTL = 0;
		TA0CCTL0 = 0;
		 P1IFG &= ~BIT0;
break;
 }
 _BIS_SR(GIE);
}



///////////con ga quang

